/**
 * 以promise封装getsetting函数
 */

export const getSetting = ()=> {
  return new Promise((resolve,reject)=> {
    wx.getSetting({
      success: function(res) {
        resolve(res);
      },
      fail: function(err) {
        reject(err);
      },
    })
  })
}

/**
 * 以promise封装chooseAddress函数
 */

export const chooseAddress = () => {
  return new Promise((resolve, reject) => {
    wx.chooseAddress({
      success: function (res) {
        resolve(res);
      },
      fail: function (err) {
        reject(err);
      },
    });
  })
}

/**
 * 以promise封装openSetting函数
 */

export const openSetting = () => {
  return new Promise((resolve, reject) => {
    wx.openSetting({
      success: function (res) {
        resolve(res);
      },
      fail: function (err) {
        reject(err);
      },
    });
  })
}

/**
 * 以promise封装showmodal函数
 */

export const showModal = (content) => {
  return new Promise((resolve, reject) => {
    wx.showModal({
      title: '提示',
      content: content.content,
      success: (res)=>  {
        resolve(res);
      },
      fail: (err)=>  {
        reject(err);
      },
    })
  })
}

/**
 * 以promise封装showToast函数
 */

export const showToast = (title) => {
  return new Promise((resolve, reject) => {
    wx.showToast({
      title: title,
      icon: 'none',
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      },
    })
  })
}

/**
 * 以promise封装wx-login函数
 */

export const login = (title) => {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout:1000,
      success: (result)=> {
        resolve(result);
      },
      fail: (err)=> {
        reject(err);
      }
    })
  })
}

/**
 * 以promise封装微信小程序支付
 * @param {object} pay：支付所必要的参数
 */

export const requestPayment = (title) => {
  return new Promise((resolve, reject) => {
    wx.requestPayment({
      ...pay,
      success: function(res) {
        resolve(res)
      },
      fail: function(err) {
        reject(err)
      },
    })
  })
}